/**
 * Package:com.sofmit.supercity.utils;
 * $Id: CompressUtil.java,v 1.2 2009/05/05 08:05:31 yangc Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * CompressUtil类，压缩与解压缩方法类
 * <p/>
 * <p><a href="CompressUtil.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: yangc $</a>
 * @version $Revision: 1.2 $
 */
public class CompressUtil {
    private static Log log = LogFactory.getLog(CompressUtil.class);
    private static int cachesize = 1024; //缓存大小
    private static Inflater decompresser = new Inflater(); //解压缩对象
    /**
     * 压缩字节数组方法，采用标准zip格式压缩
     * @param input
     * @return 压缩后字节数组
     */
    public static byte[] compressBytes(byte input[]) {
//        long start = new Date().getTime();
        Deflater deflater = new Deflater();
        deflater.reset();
        deflater.setInput(input);
        deflater.finish();
        byte[] output = new byte[0];
        ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
        try {
            byte[] buf = new byte[cachesize];
            int got;
            while (!deflater.finished()) {
                got = deflater.deflate(buf);
                o.write(buf, 0, got);
            }
            output = o.toByteArray();
        } finally {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        long end = new Date().getTime();
//        log.debug("压缩耗时：" + (end-start) + " 毫秒"+ "!压缩后大小:" + output.length);
        return output;
    }

    /**
     * 解压缩字节数组方法，采用标准zip格式解压缩
     * @param input
     * @return 解压缩后的字节数组
     */
    public static byte[] decompressBytes(byte input[]) {
        byte output[] = new byte[0];
        decompresser.reset();
        decompresser.setInput(input);
        ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
        try {
            byte[] buf = new byte[cachesize];
            int got;
            while (!decompresser.finished()) {
                got = decompresser.inflate(buf);
                o.write(buf, 0, got);
            }
            output = o.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output;
    }
    
    public static void main(String[] args) throws Exception{
//    	String str = "<sdfghjgfds>dsfghjwasd</sdfghjgfds>";
//    	byte[] b = CompressUtil.compressBytes(str.getBytes());
//    	File f = new File("d:\\temp.tmp");
//    	FileOutputStream fos = new FileOutputStream(f);
//    	fos.write(b);
//    	fos.close();
    	FileUtil.writeCompressFile("d:\\ipmsg.log", "d:\\temp.tmp");
        FileUtil.writeUncompressFile("d:\\temp.tmp", "d:\\temp.txt");
    }
    
}
